package com.xpertline.ui.config;

import java.awt.Color;
import java.awt.Font;
import java.util.Date;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.Binding.SyncFailure;
import org.jdesktop.beansbinding.BindingListener;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.PropertyStateEvent;
import org.joda.time.LocalTime;

import com.mimi.entity.Company;
import com.mimi.util.ConfigProvider;
import com.xpertline.ui.factory.CommonLabelFactory;
import com.xpertline.ui.factory.CommonTextAreaFieldFactory;
import com.xpertline.ui.factory.CommonTextFieldFactory;
import com.xpertline.ui.factory.CommonUIProperties;
import com.xpertline.ui.factory.TimeSpinnerFactory;
import com.xpertline.ui.validator.Length40Validator;

public class CompanyPane extends JPanel implements BindingListener{
	private static final long serialVersionUID = 1L;
	private JTextField tfdCompanyName;
	private JTextField tfdTel;
	private JTextField tfdFax;
	private JTextField tfdWebsite;
	private JTextField tfdEmail;
	private JTextField tfdShopName;
	private JTextField tfdShopTel;
	/**
	 * @wbp.nonvisual location=21,-21
	 */
	private  Company company = new Company();

	@SuppressWarnings("deprecation")
	public Company getCompany() {
		Date startDate = (Date)spinnerStartTime.getValue();
		LocalTime startTime = new LocalTime(startDate.getHours(), startDate.getMinutes());
		Date endDate = (Date)spinnerEndTime.getValue();
		LocalTime endTime = new LocalTime(endDate.getHours(), endDate.getMinutes());
		company.setStartTime(startTime);
		company.setEndTime(endTime);
		return company;
	}
	private JTextArea textAreaAddress;
	private JTextArea textAreaShopAddress;
	private JSpinner spinnerStartTime;
	private JSpinner spinnerEndTime;

	/**
	 * Create the panel.
	 */
	public CompanyPane() {
		initData();
		JLabel lblTnCngTy = CommonLabelFactory.createCommonJLabel();
		lblTnCngTy.setText("T\u00EAn c\u00F4ng ty");
		
		tfdCompanyName = CommonTextFieldFactory.createJTextField();
		
		JLabel lblaCh = CommonLabelFactory.createCommonJLabel();
		lblaCh.setText("\u0110\u1ECBa ch\u1EC9");
		
		textAreaAddress = CommonTextAreaFieldFactory.createJTextArea();
		
		JLabel lblTel = CommonLabelFactory.createCommonJLabel();
		lblTel.setText("TEL");
		
		tfdTel = CommonTextFieldFactory.createJTextField();
		
		JLabel lblFax = CommonLabelFactory.createCommonJLabel();
		lblFax.setText("FAX");
		
		tfdFax = CommonTextFieldFactory.createJTextField();
		
		JLabel lblWebsite = CommonLabelFactory.createCommonJLabel();
		lblWebsite.setText("Website");
		
		tfdWebsite = CommonTextFieldFactory.createJTextField();
		
		JLabel lblEmail = CommonLabelFactory.createCommonJLabel();
		lblEmail.setText("Email");
		
		tfdEmail = CommonTextFieldFactory.createJTextField();
		
		JPanel panel = new JPanel();
		panel.setFont(new Font("Arial", Font.PLAIN, 12));
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Th\u00F4ng tin c\u1EEDa ti\u1EC7m ch\u00EDnh", TitledBorder.LEADING, TitledBorder.TOP, CommonUIProperties.FONT, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(34)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblEmail)
								.addComponent(lblWebsite)
								.addComponent(lblFax)
								.addComponent(lblTel)
								.addComponent(lblaCh)
								.addComponent(lblTnCngTy))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(tfdCompanyName, GroupLayout.PREFERRED_SIZE, 336, GroupLayout.PREFERRED_SIZE)
									.addComponent(textAreaAddress, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 336, GroupLayout.PREFERRED_SIZE))
								.addComponent(tfdEmail, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfdTel, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfdWebsite, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfdFax, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(43)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 521, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(268, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(40)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTnCngTy)
						.addComponent(tfdCompanyName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblaCh)
						.addComponent(textAreaAddress, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE))
					.addGap(9)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTel)
						.addComponent(tfdTel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblFax)
						.addComponent(tfdFax, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblWebsite)
						.addComponent(tfdWebsite, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblEmail)
						.addComponent(tfdEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(12)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 209, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		JLabel lblTnTim = CommonLabelFactory.createCommonJLabel();
		lblTnTim.setText("T\u00EAn ti\u1EC7m");
		
		tfdShopName = CommonTextFieldFactory.createJTextField();
		
		JLabel lblaCh_1 = CommonLabelFactory.createCommonJLabel();
		lblaCh_1.setText("\u0110\u1ECBa ch\u1EC9");
		
		textAreaShopAddress = CommonTextAreaFieldFactory.createJTextArea();
		
		JLabel lblTel_1 = CommonLabelFactory.createCommonJLabel();
		lblTel_1.setText("TEL");
		
		tfdShopTel = CommonTextFieldFactory.createJTextField();
		
		JLabel lblThiGianHot = CommonLabelFactory.createCommonJLabel();
		lblThiGianHot.setText("Th\u1EDDi gian ho\u1EA1t \u0111\u1ED9ng");
		
		JLabel label = CommonLabelFactory.createCommonJLabel();
		label.setText("~");
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(72)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblTel_1)
						.addComponent(lblaCh_1)
						.addComponent(lblTnTim))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(tfdShopName, GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
						.addComponent(textAreaShopAddress, GroupLayout.PREFERRED_SIZE, 324, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
							.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
								.addComponent(spinnerStartTime, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
								.addGap(32)
								.addComponent(spinnerEndTime))
							.addComponent(tfdShopTel, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE)))
					.addGap(55))
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblThiGianHot)
					.addGap(85)
					.addComponent(label, GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)
					.addGap(289))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(14)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(tfdShopName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTnTim))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textAreaShopAddress, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblaCh_1))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(tfdShopTel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTel_1))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblThiGianHot)
						.addComponent(label)
						.addComponent(spinnerStartTime, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(spinnerEndTime, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(7))
		);
		panel.setLayout(gl_panel);
		setLayout(groupLayout);
		
		initDataBindings();
	}
	private void initData() {
		List<Company> companies = ConfigProvider.getInstance().getDaoHelper().getDao(Company.class).findAll();
		if(companies.size() > 0) {
			company = companies.get(0);
			spinnerStartTime = TimeSpinnerFactory.createJTimeSpinner(company.getStartTime().getHourOfDay(), company.getStartTime().getMinuteOfHour());
			spinnerEndTime = TimeSpinnerFactory.createJTimeSpinner(company.getEndTime().getHourOfDay(), company.getEndTime().getMinuteOfHour());
		} else {
			company = new Company();
			//init start time and end time
			spinnerStartTime = TimeSpinnerFactory.createJTimeSpinner(0, 0);
			spinnerEndTime = TimeSpinnerFactory.createJTimeSpinner(0, 0);
		}
	}
	@Override
	public void bindingBecameBound(Binding arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void bindingBecameUnbound(Binding arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void sourceChanged(Binding arg0, PropertyStateEvent arg1) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void syncFailed(Binding arg0, SyncFailure arg1) {
		JComponent component = (JComponent) arg0.getTargetObject();
		component.setBackground(Color.yellow);
		component.setToolTipText(arg1.getValidationResult().getDescription());
	}
	@Override
	public void synced(Binding arg0) {
		JComponent component = (JComponent) arg0.getTargetObject();
		component.setBackground(Color.white);
		component.setToolTipText("");
	}
	@Override
	public void targetChanged(Binding arg0, PropertyStateEvent arg1) {
		// TODO Auto-generated method stub
		
	}
	protected void initDataBindings() {
		BeanProperty<Company, String> companyBeanProperty = BeanProperty.create("companyName");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<Company, String, JTextField, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty, tfdCompanyName, jTextFieldBeanProperty, "CompanyName");
		autoBinding.setValidator(new Length40Validator());
		autoBinding.bind();
		//
		BeanProperty<Company, String> companyBeanProperty_1 = BeanProperty.create("address");
		BeanProperty<JTextArea, String> jTextAreaBeanProperty = BeanProperty.create("text");
		AutoBinding<Company, String, JTextArea, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty_1, textAreaAddress, jTextAreaBeanProperty, "address");
		autoBinding_1.bind();
		//
		BeanProperty<Company, String> companyBeanProperty_2 = BeanProperty.create("phone");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_1 = BeanProperty.create("text");
		AutoBinding<Company, String, JTextField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty_2, tfdTel, jTextFieldBeanProperty_1, "tel");
		autoBinding_2.bind();
		//
		BeanProperty<Company, String> companyBeanProperty_3 = BeanProperty.create("fax");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_2 = BeanProperty.create("text");
		AutoBinding<Company, String, JTextField, String> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty_3, tfdFax, jTextFieldBeanProperty_2, "fax");
		autoBinding_3.bind();
		//
		BeanProperty<Company, String> companyBeanProperty_4 = BeanProperty.create("homepage");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_3 = BeanProperty.create("text");
		AutoBinding<Company, String, JTextField, String> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty_4, tfdWebsite, jTextFieldBeanProperty_3, "website");
		autoBinding_4.bind();
		//
		BeanProperty<Company, String> companyBeanProperty_5 = BeanProperty.create("email");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_4 = BeanProperty.create("text");
		AutoBinding<Company, String, JTextField, String> autoBinding_5 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty_5, tfdEmail, jTextFieldBeanProperty_4, "email");
		autoBinding_5.bind();
		//
		BeanProperty<Company, String> companyBeanProperty_6 = BeanProperty.create("shopName");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_5 = BeanProperty.create("text");
		AutoBinding<Company, String, JTextField, String> autoBinding_6 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty_6, tfdShopName, jTextFieldBeanProperty_5, "shopname");
		autoBinding_6.bind();
		//
		BeanProperty<Company, String> companyBeanProperty_7 = BeanProperty.create("shopAddress");
		BeanProperty<JTextArea, String> jTextAreaBeanProperty_1 = BeanProperty.create("text");
		AutoBinding<Company, String, JTextArea, String> autoBinding_7 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty_7, textAreaShopAddress, jTextAreaBeanProperty_1, "shopAddress");
		autoBinding_7.bind();
		//
		BeanProperty<Company, String> companyBeanProperty_8 = BeanProperty.create("shopPhone");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_6 = BeanProperty.create("text");
		AutoBinding<Company, String, JTextField, String> autoBinding_8 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, company, companyBeanProperty_8, tfdShopTel, jTextFieldBeanProperty_6, "shopphone");
		autoBinding_8.bind();
	}
}
